package com.wipz.eoa.iphone.seal;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.toft.core2.dao.SqlUtils;
import com.toft.utils.UUIDHexGenerator;
import com.wipz.eoa.iphone.BaseAction;
import com.wipz.eoa.iphone.businessProcess.AgencyCoreService;
import com.wipz.eoa.iphone.businessProcess.TaskTransitionService;
import com.wipz.eoa.iphone.util.Constants;
import com.wipz.eoa.iphone.util.DateUtil;
/**
 * 
 * @author ljshang
 *
 */
public class SealApplyAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private ISealApplyService sealApplyService;
	private List sealApplyList;//用印申请列表
	private Map detail;// 用印申请详情
	private AgencyCoreService agencyCoreService;
	private TaskTransitionService taskTransitionService;
	private Map bxSubmit;
	private Map insert;
	
	public Map getInsert() {
		return insert;
	}
	public void setInsert(Map insert) {
		this.insert = insert;
	}
	public Map getBxSubmit() {
		return bxSubmit;
	}
	public void setBxSubmit(Map bxSubmit) {
		this.bxSubmit = bxSubmit;
	}
	public void setTaskTransitionService(TaskTransitionService taskTransitionService) {
		this.taskTransitionService = taskTransitionService;
	}
	public void setAgencyCoreService(AgencyCoreService agencyCoreService) {
		this.agencyCoreService = agencyCoreService;
	}
	public void setSealApplyService(ISealApplyService sealApplyService) {
		this.sealApplyService = sealApplyService;
	}
	public ISealApplyService getSealApplyService() {
		return sealApplyService;
	}
	public void setSealApplyList(List sealApplyList) {
		this.sealApplyList = sealApplyList;
	}
	public List getSealApplyList() {
		return sealApplyList;
	}
	public void setDetail(Map detail) {
		this.detail = detail;
	}
	public Map getDetail() {
		return detail;
	}
	/**
	 * 用印申请列表
	 *  @return
	 * @throws UnsupportedEncodingException
	 */
	public String sealApplyList()throws UnsupportedEncodingException {
		String sealname=this.getRequest().getParameter("sealname");
		try {
			String s = new String(sealname.getBytes("iso-8859-1"),"utf-8");
			sealname= java.net.URLDecoder.decode(s, "utf-8");
		} catch (UnsupportedEncodingException e) {
			return SUCCESS;
		}
		String COMMITERCODE = this.getRequest().getParameter("COMMITERCODE");
		if(COMMITERCODE.isEmpty()) {
			return SUCCESS;
		}
		Map<String,String>	parms = new HashMap<String,String>();
		parms.put("sealname", sealname);
		parms.put("COMMITERCODE", COMMITERCODE);
		sealApplyList=sealApplyService.queryList("seal.queryapply", parms);
		return SUCCESS;
		
	}
	
	/**
	 * 查询客户详情信息
	 * @return
	 * 2012-1-13 05:10:18
	 */
	public String detail() {
		String id = this.getRequest().getParameter("id");
		String COMMITERCODE = this.getRequest().getParameter("COMMITERCODE");
		String taskId = this.getRequest().getParameter("taskId");
		if(id.isEmpty()) {
			return SUCCESS;
		}
			Map<String, String> params = new HashMap();
			params.put("id", id);
			detail=(Map) sealApplyService.queryOne("seal.queryApplyById", params);
			
			Map newMap = (Map) sealApplyService.queryOne("businessProcess.queryByCode",detail);
			String processId = newMap == null ? "":(String)newMap.get("PROCESSID") ;
			detail.put("processInstanceId", processId);
			if(null == COMMITERCODE || COMMITERCODE.isEmpty()) {
				return SUCCESS;
			}
			String userId = (String) sealApplyService.queryOne("user.queryId",COMMITERCODE);
			detail = agencyCoreService.agencyList(Constants.SEALAPPLY_BUSINESS_ID, userId,detail);
			
			if(null == taskId || taskId.isEmpty()) {
				return SUCCESS;
			}
			
			detail.put("taskId", taskId);
		
			List list =(taskTransitionService.getTaskTransition(taskId));
			Map url = new HashMap();
			StringBuffer bf = new StringBuffer();
			bf.append("/seal/sealApply_bxSubmit.action?id=");
			bf.append(id);
			bf.append("&COMMITERCODE=");
			bf.append(COMMITERCODE);
			bf.append("&taskId=");
			bf.append(taskId);
			url.put("url", bf.toString());
			list.add(url);
			detail.put("button", list);
			return SUCCESS;
	}
	
	public String insert() {
		Map<String,String> params = new HashMap<String,String>();
		String id = UUIDHexGenerator.getInstance().generate();
		String billcode = Constants.getBillCode(Constants.SEAL_APPLY);
		params.put("BILLCODE", billcode); //生成单据CODE
		params.put("ID", id);
		params.put("PROCESS_STATUS", "0");
		String  COMMITERCODE = (String) this.getSession().get(Constants.USERINFO_USERCODE);
		params.put("COMMITERCODE",COMMITERCODE);
		params.put("COMMITER",this.getRequest().getParameter("COMMITER"));
		params.put("APPLYDATE", DateUtil.currDateTime());
		String sealname = this.getRequest().getParameter("SEALNAME");
		params.put("SEALNAME",sealname );
		params.put("QUANTITY", this.getRequest().getParameter("QUANTITY"));
		params.put("DESCRIBE", this.getRequest().getParameter("DESCRIBE"));
		params.put("DEPARTMENT", this.getRequest().getParameter("DEPARTMENT"));
		String agencyId = this.getRequest().getParameter("AGENCY");
		params.put("OWNERDEPT", agencyId);
		
		params.put("TRUSTEESCODE", this.getRequest().getParameter("TRUSTEESCODE"));
		params.put("TRUSTEES", this.getRequest().getParameter("TRUSTEES"));
		params.put("SEALTYPE", this.getRequest().getParameter("SEALTYPE"));
		
		 sealApplyService.insert("seal.insertapply", params);
		
		String nextApprovaler = sealApplyService.submit(Constants.SEALAPPLY_BUSINESS_ID, id, billcode, COMMITERCODE, agencyId, sealname);
		
		Map map = new HashMap();
		//设置下级处理人
		map.put("NEXTONE", nextApprovaler);
		map.put("ID", (String)params.get("ID"));
		// 修改审批状态为已提交
		map.put("PROCESS_STATUS", Constants.PROCESS_STATUS.APPROVAL.getValue());
		sealApplyService.update("seal.updateProcessStatus", map);
		
		insert = new HashMap();
		insert.put("success", true);
		insert.put("NEXTOPERATOR", nextApprovaler);
		return SUCCESS;
	}
	
	/**
	 * 用印申请审批
	 * @return
	 * 2012-2-21 下午04:41:08
	 */
	public String bxSubmit() {
		//获取ID
		String id = this.getRequest().getParameter("id");
		//用户代号
		String COMMITERCODE = this.getRequest().getParameter("COMMITERCODE");
		if(id.isEmpty() || COMMITERCODE.isEmpty()) {
			return SUCCESS;
		}
		//任务唯一标识taskId
		String taskId =  this.getRequest().getParameter("taskId");
		//处理意见
		String commentContent =  this.getRequest().getParameter("comment");
		try {
			String s = new String(commentContent.getBytes("iso-8859-1"),"utf-8");
			commentContent = java.net.URLDecoder.decode(s, "utf-8");
		} catch (UnsupportedEncodingException e) {
			return SUCCESS;
		}

		//流向
		String transition = this.getRequest().getParameter("name");
		//处理类型
		String typeaccept = this.getRequest().getParameter("pass");
		
		Map param = new HashMap();
		param.put("id",id );
		Map fixedAssetsMap =(Map)sealApplyService.queryOne("seal.queryApplyById", param);
		Map processidMap =(Map)sealApplyService.queryOne("businessProcess.queryByCode",fixedAssetsMap);
		param.put("PROCESSID", processidMap.get("PROCESSID"));
		param.put("COMMITERCODE",COMMITERCODE );
		param.put("taskId",taskId );
		param.put("comment",commentContent );
		param.put("transition",transition );
		param.put("pass",typeaccept );
		Map mapUuid = (Map) sealApplyService.queryOne("user.queryAll", COMMITERCODE);
		param.put("uuitId",mapUuid.get("ORGID"));
		bxSubmit = sealApplyService.bxSubmit(param);
		return SUCCESS;
	}
	
	public String sent() {
		String commitercode = this.getRequest().getParameter("COMMITERCODE");
		String usercode = (String) this.getSession().get(Constants.USERINFO_USERCODE);
		if(null == usercode || "".equals(usercode)) {
		this.getSession().put(Constants.USERINFO_USERCODE, commitercode);
		}
		return SUCCESS;
	}

}
